package com.vsoft.sms_cm;

import java.io.DataOutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.log4j.Logger;

import com.vsoft.framework.core.BaseException;
import com.vsoft.framework.core.BusiObj;
import com.vsoft.framework.core.Configuration;
import com.vsoft.framework.core.db.DBGadgets;
import com.vsoft.framework.core.db.DBTrans;
import com.vsoft.framework.core.esb.ServiceBus;
import com.vsoft.framework.core.util.BOHelper;
import com.vsoft.framework.core.util.LogCollect;
import com.vsoft.framework.core.util.XMLUtil;
import com.vsoft.framework.server.servlet.SessionData;
import com.vsoft.framework.server.servlet.HttpUtil;
import com.vsoft.anti_fra.*;

public class TestCRMInterface {

	private static Logger vLogger = Logger.getLogger(TestQuery.class);
	private static String vName = TestCRMInterface.class.getName();
	private SessionData vSD = null;
	private String vDebugHome = "";
	private BusiObj				vInBO		=  new BusiObj();
	
	
	public static void main(String[] args)  {
	
		try{
/*			response.reset();
			   response.setCharacterEncoding("GBK");
			   response.setContentType("application nd.ms-excel;charset=GBK");
			   String fileName = new String("通讯录.xls".getBytes("GBK"), "iso-8859-1");      //设置导出文件中文名编码
			   response.setHeader("content-disposition", "attachment; filename=" + fileName);
			   DataOutputStream stream = new DataOutputStream(response.getOutputStream());
			   workbook.write(stream);
			   stream.flush();
			   stream.close();*/
			vLogger.info("---------------InitSystem-------------");
			Configuration.InitSystem();
			vLogger.info("---------------generate test data-------------");
/*			String order_num="11111";
			Connection conn1=DBTrans.getConnection();
			//Connection conn1=DBTrans.getConnection("sms_in");
			Customer.getSubscriberAndSMSTemple(order_num, conn1);*/
			TestCRMInterface t = new TestCRMInterface();
			//TestQuery t = new TestQuery();
			Connection conn = DBTrans.getConnection();
			//t.vInBO.setString("order_num", "05552356300");
			//t.vInBO.setName("getSubscriberAndSMSTemple");//AF.
			//////////////////////////////////////客户信息
			BusiObj		enterpriseBO	=new BusiObj() ;
			enterpriseBO.setString("ent_code", "-1");
			enterpriseBO.setString("cust_id", "1");
			enterpriseBO.setString("ent_name", "测试企业1");
			enterpriseBO.setString("mobile_num", "13977770101");
			enterpriseBO.setString("phone_num", "88888888");
			enterpriseBO.setString("Social_Id", "430101198001010013");
			enterpriseBO.setString("area_code", "0551");
			enterpriseBO.setString("industry_type", "1");
			enterpriseBO.setString("Social_Type_Id", "91A");
			enterpriseBO.setString("state", "1");
			enterpriseBO.setString("email", "");
			enterpriseBO.setString("address", "xxxxxxx");
			enterpriseBO.setString("password", "123");
			enterpriseBO.setString("postcode", "430101");
			enterpriseBO.setString("new_flag", "1");
			t.vInBO.setValue("enterprise", enterpriseBO);
			/////////////////////////////////////////////订单信息
			BusiObj		orderBO	=new BusiObj() ;
			orderBO.setString("order_sn", "-1");
			orderBO.setString("offer_id", "10023");
			orderBO.setString("area_code", "0551");
			orderBO.setString("VOUCH_NUMBER", "3659663");
			orderBO.setString("ent_code", "-1");
			orderBO.setString("order_time", "");
			orderBO.setString("order_no", "-1");
			orderBO.setString("notes", "测试订购_1");
			orderBO.setString("new_flag", "1");
			t.vInBO.setValue("order_Info", orderBO);
			/////////////////////////////////////////////定价计划实例信息
			BusiObj		pricePlanInstBO	=new BusiObj() ;
			pricePlanInstBO.setString("SUBS_UPP_INST_ID", "-1");
			pricePlanInstBO.setString("SUBS_ID", "-1");
			pricePlanInstBO.setString("CREATED_DATE", "");
			pricePlanInstBO.setString("EFF_DATE", "");
			pricePlanInstBO.setString("EXP_DATE", "");
			pricePlanInstBO.setString("STATE", "1");
			pricePlanInstBO.setString("STATE_DATE", "");
			pricePlanInstBO.setString("UPDATE_DATE", "");
			pricePlanInstBO.setString("OFFER_ID", "10023");
			pricePlanInstBO.setString("PRICE_PLAN_ID", "10023");
			pricePlanInstBO.setString("COMPLETED_DATE", "");
			pricePlanInstBO.setString("new_flag", "1");
			t.vInBO.setValue("PRICE_PLAN_INST", pricePlanInstBO);
/*			/////////////////////////////////////////////offer实例信息
			BusiObj		offerBO	=new BusiObj() ;
			BusiObj		offerInstanceBO	=new BusiObj() ;
			offerInstanceBO.setString("offer_inst_sn", "-1");
			offerInstanceBO.setString("offer_id", "4");
			offerInstanceBO.setString("order_sn", "-1");
			offerInstanceBO.setString("state", "1");
			offerInstanceBO.setString("notes", "first offer");
			offerInstanceBO.setString("new_flag", "1");
			t.vInBO.setValue("offer_Instance", offerInstanceBO);
			//
			BusiObj		productInstanceBO	=new BusiObj() ;
			productInstanceBO.setString("offer_inst_sn", "-1");
			productInstanceBO.setString("prod_inst_sn", "-1");
			productInstanceBO.setString("product_id", "68");
			productInstanceBO.setString("state", "1");
			productInstanceBO.setString("new_flag", "1");
			t.vInBO.setValue("product_Instance", productInstanceBO);
			//
			ArrayList<BusiObj> aPropsInst = new ArrayList<BusiObj> ();
			BusiObj		productPropsInstSmsBO	=new BusiObj() ;
			productPropsInstSmsBO.setString("props_inst_sn", "-1");
			productPropsInstSmsBO.setString("prod_inst_sn", "-1");
			productPropsInstSmsBO.setString("state", "1");
			productPropsInstSmsBO.setString("work_order_no", "-1");
			productPropsInstSmsBO.setString("order_no", "-1");
			productPropsInstSmsBO.setString("props_inst_name", "挂机号码");//props_inst_value
			productPropsInstSmsBO.setString("props_inst_value", "073188888888");//props_inst_value
			productPropsInstSmsBO.setString("props_id", "86");
			productPropsInstSmsBO.setString("new_flag", "1");
			aPropsInst.add(productPropsInstSmsBO);
			BusiObj		productPropsInstSmsBO1	=new BusiObj() ;
			productPropsInstSmsBO1.setString("props_inst_sn", "-2");
			productPropsInstSmsBO1.setString("prod_inst_sn", "-1");
			productPropsInstSmsBO1.setString("state", "1");
			productPropsInstSmsBO1.setString("order_no", "-1");
			productPropsInstSmsBO1.setString("work_order_no", "-2");
			productPropsInstSmsBO1.setString("props_inst_name", "免打扰号码");
			productPropsInstSmsBO1.setString("props_inst_value", "13988888888");//props_inst_value
			productPropsInstSmsBO1.setString("props_id", "87");
			productPropsInstSmsBO1.setString("new_flag", "1");
			aPropsInst.add(productPropsInstSmsBO1);
			t.vInBO.setValue("product_props_inst_sms", aPropsInst);*/
			/////////////////////////////////////////////call service
			t.vInBO.setString("new_flag", "1");
			t.vInBO.setConnection(conn);
			t.vInBO.setName("Accept");
			t.vInBO.setString("SESSION_STAFF_NO", "999");
			BusiObj retBo					=		ServiceBus.runService(t.vInBO);
			DBGadgets.check(conn);
			conn.rollback();
			//conn.commit();
			conn.close();
			vLogger.info("返回数据是:\n\n\n\n" + BOHelper.boToXML(retBo));
/*			t.vInBO.setString("start_time", "2010-10-30 21:30:00");
			t.vInBO.setString("end_time", "2010-11-30 21:30:00");*/
			
/*			vLogger.info("---------------run test data-------------");
			Customer server = new Customer();*/
			//server.execute(t.vInBO);
			
			//DBGadgets.check(conn);
			
/*			t.vInBO.setString("start_time", "2010-10-30 21:30:00");
			t.vInBO.setString("end_time", "2010-11-30 21:30:00");*/
			
			//t.vInBO.setString("call_num", "18955179099");
			//t.vInBO.setName("AF_GetIvrCountByAreaCallnum");//
			//t.vInBO.setName("AF_GetIvrList");
			//t.vInBO.setName("AF_GetExpDetList");
/*			t.vInBO.setString("pageRows", "40");//
			t.vInBO.setString("only110_flag", "1");//
			t.vInBO.setString("totalRows", "144040");*/
			//vInBO=server.execute(t.vInBO);
/*			vLogger.info("---------------导出数据查询完成-------------" +
					"\n导出xls开始");
			////////////////////////////////
			//准备参数bo
			//vInBO1.setName( "AF_GetIvrCountByArea");//
			ArrayList<String> alServName	=	new ArrayList<String>();
			alServName.add("AF_GetIvrCountByArea");
			alServName.add("AF_GetIvrList");
			alServName.add("AF_GetExpDetList");
			alServName.add("AF_GetIvrCountByAreaCallnum");
			int	iServ	=1;
			vInBO1.setName(alServName.get(iServ));
			vInBO1.setString("pageRows", "100000");//search_text
			t.vInBO.setString("totalRows", "144040");
			vInBO1.setString("service_name",alServName.get(iServ));
			//vInBO1.setString("expAllFlag","1");
			vInBO.setValue("PARAMS", vInBO1);
			/////////////////////////////////////////
			//准备列名对照
			BusiObj		colHeaderBO	=	t.getColHeader(iServ);
			vInBO.setValue("COLHEADER", colHeaderBO);
			/////////////////////////////////////////
			vInBO.setConnection(conn);//EXPORT_XLS
			vInBO.setName("AF_EXPORT_XLS");//
			FileExport FileExport = new FileExport();
			FileExport.execute(vInBO);
			vLogger.info("返回数据是:\n\n\n\n" + BOHelper.boToXML(vInBO));
			vLogger.info("---------------导出xls完成-------------" );*/
			//////////////////////////////

		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		catch(BaseException e)
		{
			e.printStackTrace();
			vLogger.error(e.ErrMethod);
			vLogger.error(e.SysMsg);
		}
			vLogger.info("---------------Game Over-------------");
	}	
	private BusiObj	getColHeader(int servName)
	{
/*		alServName.add("AF_GetIvrCountByArea");
		alServName.add("AF_GetIvrList");
		alServName.add("AF_GetExpDetList");
		alServName.add("AF_GetIvrCountByAreaCallnum");*/
		//HashMap<String,Object> colHeader	=	new	HashMap<String,Object>();
		BusiObj				colHeader		=  new BusiObj();
		BusiObj				headerBO		=  new BusiObj("colheader");
		switch(servName)
		{
		case 0:	//
			colHeader.setString("INVALID_IVR_COUNT", "未接听次数");
			colHeader.setString("CALLED_AREA", "区域");
			colHeader.setString("VALID_IVR_COUNT", "接听次数");
			colHeader.setString("IVR_COUNT", "外呼次数");
			break;
		case 1:	//
			colHeader.setString("CALLING_NUM", "欺诈号码");
			colHeader.setString("CALLING_ATTR", "呼叫属性");
			colHeader.setString("CALLED_NUM", "被叫号码");
			colHeader.setString("CALLED_ATTR", "被叫属性");
			colHeader.setString("CALLING_AREA", "主叫区域");
			colHeader.setString("CALLED_AREA", "被叫区域");
			colHeader.setString("CALLING_PHY_NUM", "主叫物理号码");
			colHeader.setString("STATUS", "外呼状态");
			colHeader.setString("IVRCALL_SDATE", "外呼时间");
			break;
		case 2:	//
			colHeader.setString("id", "序号");
			colHeader.setString("callnum", "疑似号码");
			colHeader.setString("state", "状态");
			colHeader.setString("state_time", "记录时间");
			break;
		case 3:
			colHeader.setString("INVALID_IVR_COUNT", "未接听次数");
			colHeader.setString("VALID_IVR_COUNT", "接听次数");
			colHeader.setString("IVR_COUNT", "外呼次数");
			colHeader.setString("CALLED_AREA", "区域");
			colHeader.setString("CALLING_NUM", "欺诈号码");
			break;
		}
		return colHeader;
	}
		

	private int  init()
	{
		int iRet = 0;
		vSD = new  SessionData();
		vSD.vRoleCode = new String[] {"1"};
		vSD.vStaffCode = "1";
		vSD.vStaffNo = "0001";
		vSD.vStaffName = "test";
		
	
		vDebugHome = System.getProperty("DEBUG_HOME");
		if (vDebugHome == null)
		{
			iRet = -1;
			vLogger.error("需要设置DEBUG_HOME环境变量来指定debug 文件所在的目录");
		}
		return iRet;
		
	}
	
}
